package popcorn.ticket.serviceimpl;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import popcorn.ticket.dao.MembersMapper;
import popcorn.ticket.dao.UsersMapper;
import popcorn.ticket.entity.Members;
import popcorn.ticket.entity.Users;
import popcorn.ticket.entity.UsersExample;
import popcorn.ticket.service.UsersService;
import popcorn.ticket.tool.MD5Util;
import popcorn.ticket.tool.PageData;

@Service
public class UsersServiceImpl implements UsersService {
	@Autowired
	UsersMapper usersmapper;
	@Autowired
	MembersMapper membersmapper;

	Logger logger = Logger.getLogger(UsersServiceImpl.class);

	// 分页
	@Override
	public PageData<Users> selectUsersByLike(int page, int limit, String keywords) {
		String name = "%" + keywords + "%";
		// ue方法查询
		UsersExample ue = new UsersExample();
		PageHelper.startPage(page, limit);

		ue.createCriteria().andUsernameLike(name);
		List<Users> data = usersmapper.selectByExample(ue);
		// 实例化分页信息
		PageInfo<Users> pageInfo = new PageInfo<Users>(data);
		// 构造方法 赋值内容为 数据 总记录数 limit page
		PageData<Users> pageData = new PageData<>();
		// 获得数据总数
		pageData.setCount((int) pageInfo.getTotal());
		pageData.setData(data);
		pageData.setLimit(limit);
		pageData.setPage(page);
		// 还有: code 从layui哪里来的
		pageData.setCode(0);
		pageData.setMsg("");

		return pageData;

	}

	// 修改
	@Override
	@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.READ_COMMITTED)
	public int updateByPrimaryKeySelective(Users users) {
		if (users.getUserpwd() != null) {
			users.setUserpwd(MD5Util.getEncodeByMd5(users.getUserpwd()));
		}
		int n = usersmapper.updateByPrimaryKeySelective(users);
		return n;
	}

	// 删除,事务
	@Override
	@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.READ_COMMITTED)
	public int delUsersById(int userid) {
		int n = usersmapper.deleteByPrimaryKey(userid);
		return n;
	}

	// 增加,事务
	@Override
	@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.READ_COMMITTED)
	public int addUsers(Users users) {
		// 初始化x=0
		int x = 0;
		// 判断会员添加是否为false
		int y = 0;
		// 先判断用户名和手机号是否存在
		Users list1 = usersmapper.selectByName(users.getUsername());

		Users list2 = usersmapper.selectByTel(users.getUsertel());

		if (list1 != null) {
			x = 1;
		} else if (list2 != null) {
			x = 4;
		} else if (list1 == null && list2 == null) {
			// 加密处理
			String userpwd = MD5Util.getEncodeByMd5(users.getUserpwd());
			users.setUserpwd(userpwd);
			// 首次创建用户，默认头像
			users.setUserpic("./images/header.jpg");
			x = usersmapper.insert(users);
			// 设置一个异常，如果 用户添加失败 则被除数x为0
			int j = 1;
			int z = j / x;
			System.out.println(z);
			// 若用户添加无异常，重新获取新添加的用户id
			UsersExample example = new UsersExample();
			example.createCriteria().andUsernameLike(users.getUsername());
			int userid = usersmapper.selectByExample(example).get(0).getUserid();

			// 执行会员信息添加
			Members members = new Members();
			members.setIntegral(0);
			members.setGradeid(1);
			members.setUserid(userid);
			y = membersmapper.insert(members);
		}

		return (x + y);

	}

	// 登录方法
	@Override
	public boolean login(String username, String userpwd1, HttpSession session) {
		boolean flag = false;
		// 判断用户账号
		boolean a = false;
		UsersExample example = new UsersExample();
		// 判断用户是用户名登录还是手机登录
		for (int i = username.length(); --i >= 0;) {
			if (Character.isDigit(username.charAt(i))) {
				a = true;
			}
		}
		// 如果a为true 则证明是手机号登录
		if (a) {
			example.createCriteria().andUsertelLike(username);
			List<Users> list = usersmapper.selectByExample(example);
			// 数据库获取密码
			String pwd = list.get(0).getUserpwd();
			// 获取用户输入的密码
			String userpwd = MD5Util.getEncodeByMd5(userpwd1);
			// 检查用户是否被禁止登录
			int condition = list.get(0).getUsercondition();
			// 验证密码
			if (userpwd.equals(pwd) && condition == 1) {
				int userid = list.get(0).getUserid();
				// 记住用户id
				session.setAttribute("userid", userid);
				Users u = usersmapper.selectByPrimaryKey(userid);
				session.setAttribute("username", u.getUsername());
				session.setAttribute("userpic", u.getUserpic());
				flag = true;
			}
		} else {
			example.createCriteria().andUsernameLike(username);
			List<Users> list = usersmapper.selectByExample(example);
			// 数据库获取密码
			String pwd = list.get(0).getUserpwd();
			// 获取用户输入的密码并加密
			String userpwd = MD5Util.getEncodeByMd5(userpwd1);
			// 检查用户是否被禁止登录
			int condition = list.get(0).getUsercondition();
			// 验证密码
			if (pwd.equals(userpwd) && condition == 1) {
				int userid = list.get(0).getUserid();
				String un = list.get(0).getUsername();
				// 记住用户id
				session.setAttribute("userid", userid);
				session.setAttribute("username", un);
				Users u = usersmapper.selectByPrimaryKey(userid);
				session.setAttribute("userpic", u.getUserpic());
				flag = true;
			}
		}
		return flag;
	}

	// 获取单个用户信息
	@Override
	public Users getUser(int userid) {

		return usersmapper.selectByPrimaryKey(userid);
	}

	// 判断用户权限，管理员还是普通用户
	@Override
	public int info(String username) {
		// 判断用户是用户名登录还是手机登录
		int a = 0;
		boolean b = false;
		for (int i = username.length(); --i >= 0;) {
			if (Character.isDigit(username.charAt(i))) {
				b = true;
			}
		}
		if (b) {
			a = usersmapper.selectByTel(username).getUserpower();
		} else {
			a = usersmapper.selectByName(username).getUserpower();
		}

		return a;
	};

	// 验证用户手机号是否存在
	@Override
	public boolean getInfoByTel(String usertel) {
		Users users = usersmapper.selectByTel(usertel);
		if (users == null) {
			return false;
		}
		return true;
	};
	// 修改密码
	@Override
	public boolean updPwd(String tel, String userpwd) {
		Users users = new Users();
		// 获取用户输入的密码并加密
		users.setUserpwd(MD5Util.getEncodeByMd5(userpwd));
		UsersExample ue = new UsersExample();
		ue.createCriteria().andUsertelLike(tel);
		int flag = usersmapper.updateByExampleSelective(users, ue);
		if (flag == 1) {
			return true;
		}
		return false;
	};
	
	@Override
	public PageData<Users> selectUsersByPower(int page, int limit, String keywords) {
		// ue方法查询
		
		PageHelper.startPage(page, limit);

		List<Users> data = usersmapper.selectByPower(keywords);
		// 实例化分页信息
		PageInfo<Users> pageInfo = new PageInfo<Users>(data);
		// 构造方法 赋值内容为 数据 总记录数 limit page
		PageData<Users> pageData = new PageData<>();
		// 获得数据总数
		pageData.setCount((int) pageInfo.getTotal());
		pageData.setData(data);
		pageData.setLimit(limit);
		pageData.setPage(page);
		// 还有: code 从layui哪里来的
		pageData.setCode(0);
		pageData.setMsg("");

		return pageData;
	};

}
